package com.lhh.userservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.net.HttpHeaders;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.annotation.WithRequestId;
import com.lhh.springcommon.entity.Chat;
import com.lhh.springcommon.entity.User;
import com.lhh.springcommon.entity.vo.ChatVO;
import com.lhh.springcommon.entity.vo.UserVO;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.springcommon.utils.Response;
import com.lhh.userservice.service.UserChatService;
import com.lhh.userservice.service.UserManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户数据处理
 * @author 李厚航
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    private final UserManagementService userManagementService;

    private final UserChatService userChatService;

    @Autowired
    public UserInfoController(UserManagementService userManagementService, UserChatService userChatService) {
        this.userManagementService = userManagementService;
        this.userChatService = userChatService;
    }

    /**
     * 根据用户基本查询用户信息
     * @param user
     * @return
     */
    @PostMapping("/queryUserInfo")
    @WithRequestId
    @WebLog
    public Response<UserVO> queryUserInfo(@RequestBody User user) {
        return Response.success(userManagementService.getUserVOByUser(user));
    }

    /**
     * 查询用户信息
     * @return 用户数据
     */
    @GetMapping("/query")
    @WithRequestId
    @WebLog
    public Response<UserVO> query(){
        return Response.success(userManagementService.getUserByToken());
    }

    /**
     * 查询用户未读消息数
     * @return 未读消息数
     */
    @GetMapping("/countUnread")
    @WithRequestId
    @WebLog
    public Response<Integer> countUnread(){
        return Response.success(userChatService.countUnread());
    }

    /**
     * 添加一条聊天数据
     * @param chat
     * @return
     */
    @PostMapping("/addChat")
    @WithRequestId
    @WebLog
    public Response<Chat> addChat(@RequestBody Chat chat){
        return Response.success(userChatService.add(chat));
    }

    /**
     * 分页查询聊天信息
     * @param chat
     * @return
     */
    @PostMapping("/queryPage")
    @WithRequestId
    @WebLog
    public Response<Page<Chat>> queryPage(@RequestBody PageQuery<Chat> chat){
        return Response.success(userChatService.queryPageChat(chat));
    }

    /**
     * 将消息设为已查看
     * @return
     */
    @PutMapping("/checkChat")
    @WithRequestId
    @WebLog
    public Response<String> checkChat(){
        return Response.success(userChatService.checkChat());
    }

    /**
     * 根据好友id查询信息
     * @param fid
     * @return
     */
    @GetMapping("/queryByFid")
    @WithRequestId
    @WebLog
    public Response<List<ChatVO>> queryByFid(@RequestParam("fid") Integer fid){
        return Response.success(userChatService.queryByFid(fid));
    }
}
